package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderListVO;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/order")
@Api(tags = "订单管理模块")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    /**
     * 订单提交
     * */
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO dto) {
        log.info("提交订单{}",dto);
        OrderSubmitVO vo = ordersService.submit(dto);
        return Result.success(vo);
    }
    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }
    /**
     * 历史订单查询
     * */
    @GetMapping("historyOrders")
    @ApiOperation("查询用户订单")
    public Result<PageResult> getHistoryOrders(OrdersPageQueryDTO dto) {
        log.info("用户订单查询{}",dto);
        PageResult page = ordersService.getHistoryOrders(dto);
        return Result.success(page);
    }
    /**
     * 订单详细查询
     * */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详细")
    public Result getDetailByOrderId(OrdersPageQueryDTO dto) {
        log.info("订单详细查询id={}",dto.getId());
        PageResult page = ordersService.getHistoryOrders(dto);
        List<OrderListVO> list = page.getRecords();
        return Result.success(list.get(0));
    }
    /**
     * 订单取消
     * */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable("id") Long id) {
        log.info("取消订单id={}",id);
        ordersService.cancel(id);
        return Result.success();
    }
    /**
     * 再来一单接口
     * */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetition(@PathVariable("id") Long id) {
        log.info("再来一单id={}",id);
        ordersService.repetition(id);
        return Result.success();
    }
    /**
     * 用户催单
     * */
    @ApiOperation("催单")
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable("id") Long id) {
        log.info("催单id={}",id);
        ordersService.reminder(id);
        return Result.success();
    }
}
